#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include<qrect.h>
#include<qtimer.h>
#include<QTimer>
#include<QKeyEvent>
#include<QDebug>
#include<QFont>
#include<QPixmap>
#include<QtGlobal>
#include<QTime>
#include<ctime>
#include<QPixmap>
#include<QPushButton>
#include<tools.h>
#include"assignment.h"
#include"equipmentshop.h"
#include"balance_account.h"
#include"message.h"
#include"floor2.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
//    , numberfinished(0),ui(new Ui::MainWindow),speed(50),blsRun(false),havespeed(0),canspeed(1),measurespeed(0),blsOver(false),timenokill(0),timekill(0)
//    , islight(1),staytime(0),istouch(false),iskilling(false),lightspread(100),ghostspeed(3.5),numbershengmu(0),havecolded(false),shengmu_killing(false),times1(0)
{
//    ui->setupUi(this);
//    this->setGeometry(QRect(435,100,700,700));//第三格是x轴
//    QPushButton *btn2=new QPushButton("任务栏",this);
//    btn2->move(600,15);
//    connect(btn2,&QPushButton::clicked,this,&MainWindow::gotoassignment);
//    QPushButton *btn3=new QPushButton("发车",this);
//    btn3->move(600,55);
//    connect(btn3,&QPushButton::clicked,this,&MainWindow::traindepature);
//    assignmentstore.resize(5);
//    assignmentstore[0]="逃杀";
//    assignmentstore[1]="刺骨寒温";
//    assignmentstore[2]="在鬼附近使用圣木";
//    assignmentstore[3]="在鬼猎杀时使用圣木驱散鬼魂";
//    assignmentstore[4]="踩盐";
//    setFixedSize(700,700);
}
MainWindow::MainWindow(int money1,int shengmusended1,int shengmustore1,int saltnumbertaken1,int click1,int saltstore1):
    numberfinished(0),ui(new Ui::MainWindow),speed(50),blsRun(false),havespeed(0),canspeed(1),measurespeed(0),blsOver(false),timenokill(0),timekill(0),havekilled(false),havesalt(false),
    islight(1),staytime(0),istouch(false),iskilling(false),lightspread(100),ghostspeed(3.5),numbershengmu(0),havecolded(false),shengmu_killing(false),times1(0),whichfloor(1)
{
    ui->setupUi(this);
    money=money1;
    click=click1;
    saltstore=saltstore1;
    shengmusended=shengmusended1;
    shengmustore=shengmustore1;
    saltnumbertaken=saltnumbertaken1;
    saltnumber=-1;
    salt.resize(saltnumbertaken);
    salttouch.resize(saltnumbertaken);
    saltwhichfloor.resize(saltnumbertaken);
    this->setGeometry(QRect(435,100,700,700));//第三格是x轴
    QPushButton *btn2=new QPushButton("任务栏",this);
    btn2->move(600,15);
    connect(btn2,&QPushButton::clicked,this,&MainWindow::gotoassignment);
    QPushButton *btn3=new QPushButton("发车",this);
    btn3->move(600,55);
    connect(btn3,&QPushButton::clicked,this,&MainWindow::traindepature);
    assignmentstore.resize(5);
    assignmentstore[0]="逃杀";
    assignmentstore[1]="刺骨寒温";
    assignmentstore[2]="在鬼附近使用圣木";
    assignmentstore[3]="在鬼猎杀时使用圣木驱散鬼魂";
    assignmentstore[4]="踩盐";
    setFixedSize(700,700);
    floor=new floor2();

}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::paintEvent(QPaintEvent*event)//update()或程序第一次启动时自动执行，用于画图
{
    //判断按下过几次键 控制视野的大小
    if(islight%3==2)
    {
        lightspread=100;
    }
    else if(islight%3==1)
    {
        lightspread=70;
    }
    else if(islight%3==0)
    {
        lightspread=30;
    }
    nDirection=0;//不输入就不动
    QPainter painter(this);

    //初始化
    if(blsRun==false)
    {
        InitMainWindow();
    }

    //画游戏背景


    //画小格子，大小10*10
    //painter.setPen(Qt::blue);//设置画笔颜色
    //for(int i=2;i<=58;i++)
    //{
      //  painter.drawLine(20,i*10,580,i*10);//竖线
        //painter.drawLine(i*10,20,i*10,580);//横线
    //}
    //画场内地图
    if(whichfloor==0)
    {
    painter.drawPixmap(35,35,530,530,QPixmap(":/images/myImages/floor4.png"));
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawPixmap(15,15,20,570,QPixmap(":/images/myImages/wall.png"));//1
    painter.drawPixmap(15,15,520,20,QPixmap(":/images/myImages/wall.png"));//2
    painter.drawPixmap(280,15,20,140,QPixmap(":/images/myImages/wall.png"));//3
    painter.drawPixmap(15,391,135,20,QPixmap(":/images/myImages/wall.png"));//13
    painter.drawPixmap(480,320,105,20,QPixmap(":/images/myImages/wall.png"));//8
    painter.drawPixmap(330,390,21,181,QPixmap(":/images/myImages/wall.png"));//10
    painter.drawPixmap(310,210,21,80,QPixmap(":/images/myImages/wall.png"));//11
    painter.drawPixmap(310,270,70,20,QPixmap(":/images/myImages/wall.png"));//12
    painter.drawPixmap(515,15,20,100,QPixmap(":/images/myImages/wall.png"));//5
    painter.drawPixmap(515,95,70,20,QPixmap(":/images/myImages/wall.png"));//6
    painter.drawPixmap(565,95,20,490,QPixmap(":/images/myImages/wall.png"));//7
    painter.drawPixmap(15,565,570,20,QPixmap(":/images/myImages/wall.png"));//9
    painter.drawPixmap(150,260,20,151,QPixmap(":/images/myImages/wall.png"));//4
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawRect(535,15,30,80);
    //门
    painter.drawPixmap(30,35,20,40,QPixmap(":/images/myImages/door2.png"));
    }
    else
    {
    //一层的外墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(15,15,570,570);//画矩形，相对于窗体的起点坐标（15，15），大小260*260
   // painter.drawPixmap(20,20,560,560,QPixmap(":/images/myImages/backpicture.png"));//一层的背景图
    painter.drawPixmap(20,20,560,560,QPixmap(":/images/myImages/floor5.png"));
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawPixmap(20,295,120,10,QPixmap(":/images/myImages/wall.png"));//横线1/4
    painter.drawPixmap(157,295,150,10,QPixmap(":/images/myImages/wall.png"));
    painter.drawPixmap(305,295,127,10,QPixmap(":/images/myImages/wall.png"));
    painter.drawPixmap(453,295,127,10,QPixmap(":/images/myImages/wall.png"));
    painter.drawPixmap(295,20,10,120,QPixmap(":/images/myImages/wall.png"));//竖线1/2
    painter.drawPixmap(295,157,10,145,QPixmap(":/images/myImages/wall.png"));
    painter.drawPixmap(295,305,10,127,QPixmap(":/images/myImages/wall.png"));
    painter.drawPixmap(295,453,10,127,QPixmap(":/images/myImages/wall.png"));
    //桌子
    painter.drawPixmap(120,140,80,40,QPixmap(":/images/myImages/desk.png"));
    painter.drawPixmap(400,140,80,40,QPixmap(":/images/myImages/desk.png"));
    painter.drawPixmap(140,400,40,80,QPixmap(":/images/myImages/desk1.png"));
    painter.drawPixmap(430,420,40,80,QPixmap(":/images/myImages/desk1.png"));
    //门
    if(click%2==1)
    painter.drawPixmap(545,530,20,45,QPixmap(":/images/myImages/door2.png"));
    }
    //内墙
    // painter.setPen(Qt::black);
    //painter.setBrush(Qt::black);
    //painter.drawRect(20,20,560,560);

    //画盐
    for(int i=0;i<=saltnumber;i++)
    {
        QVector<QRect> a;
        a.resize(1);
        a[0]=salt[i];
        QPoint a1(ghost.x(),ghost.y()),a2(ghost.x()+15,ghost.y()),a3(ghost.x()+15,ghost.y()+15),a4(ghost.x(),ghost.y()+15);
        if((isinRect(a1,a)!=0||isinRect(a2,a)!=0||isinRect(a3,a)!=0||isinRect(a4,a)!=0)&&ghostfloor==saltwhichfloor[i])
        {
            salttouch[i]=1;
            havesalt=true;
        }
    }
    for(int i=0;i<=saltnumber;i++)
    {
        if(whichfloor==saltwhichfloor[i])//如果放的不在同一层楼就不显示
        {
        if(salttouch[i]==0)//没踩
        painter.drawPixmap(salt[i],QPixmap(":/images/myImages/salt.png"));//
        else //踩了
        painter.drawPixmap(salt[i],QPixmap(":/images/myImages/saltstepped.png"));
        }
    }

    //画冒险家
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    //painter.drawRect(adventurer);
    painter.drawPixmap(adventurer.left(),adventurer.top(),15,15,QPixmap(":/images/myImages/picture1.png"));
    //画鬼
    if(iskilling)
    {
     painter.setPen(Qt::black);
     painter.setBrush(Qt::red);
     //painter.drawRect(ghost);
     painter.drawPixmap(ghost.left(),ghost.top(),15,15,QPixmap(":/images/myImages/picture2.png"));
    }
    //使视野只能是周身lighrspread格的方块
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    int m=0;
    if(whichfloor==0)
    {
        m=5;
    }
    if(adventurer.top()-lightspread>20-m)
    {
      painter.drawRect(20-m,20-m,560+2*m,adventurer.top()-lightspread-20+m);
    }
    if(adventurer.left()-lightspread>20-m)
    {
    painter.drawRect(20-m,20-m,adventurer.left()-lightspread-20+m,560+2*m);
    }
    if(adventurer.bottom()+lightspread<580+m)
    {
    painter.drawRect(20-m,adventurer.bottom()+lightspread,560+2*m,580+m-lightspread-adventurer.bottom());
    }
    if(adventurer.right()+lightspread<580+m)
    {
    painter.drawRect(adventurer.right()+lightspread,20-m,580+m-lightspread-adventurer.right(),560+2*m);
    }
    if((times%2==1||times==0||times==2||times==4)&&iskilling)//明暗变换
    {
        painter.setPen(Qt::black);
        painter.setBrush(Qt::black);
        painter.drawRect(15,15,570,570);
    }
    //显示游戏开始，结束
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(220,270,sDisplay);//写开始与结束
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    if(whichfloor==1)//温度
    painter.drawText(570,640,QString::number(array1[adventurer.left()-20][adventurer.top()-20]));
    else
    painter.drawText(570,640,QString::number(array2[adventurer.left()-20][adventurer.top()-20]));
    painter.setFont(font2);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(500,640,QString("温度:"));
    painter.drawText(20,640,"圣木数量");
    painter.drawText(100,640,QString::number(numbershengmu));//画圣木
    painter.drawText(150,640,"盐数量");
    painter.drawText(234,640,QString::number(saltnumbertaken-saltnumber-1));
    if(whichfloor==1)
    {
    if(array1[adventurer.left()-20][adventurer.top()-20]<0)//刺骨寒温任务
        havecolded=true;
    }
    else if(whichfloor==0)
    {
        if(array2[adventurer.left()-20][adventurer.top()-20]<4)//在第二层楼如果温度小于四则刺骨寒温任务完成
            havecolded=true;
    }
    if(blsOver)
    {
        timer->stop();
        timer1->stop();
        timer2->start(1000);
        if(times1<=3)
        {
            painter.drawPixmap(20,20,560,560,QPixmap(":/images/myImages/ghostkill_20220430211101.png"));
        }
    }
    if(times1==6)
    {
        timer2->stop();
       traindepature();
    }
}
void MainWindow::InitMainWindow()
{
    numbershengmu=shengmusended;
    times=0;
    blsRun=true;
    QRect rect(100,70,10,10);//初始冒险家
    adventurer=rect;
    adventurerSXZY.resize(4);//初始冒险家四个角的坐标
    srand(QTime(0,0,0).secsTo(QTime::currentTime()));//初始鬼坐标
    ghostix=20+rand()%(580-20);
    ghostiy=20+rand()%(580-20);
    //初始鬼房位置
    if(click%2==1)
    ghostfloor=rand()%(2-0);
    else
    ghostfloor=1;
    QRect rect0(ghostix,ghostiy,10,10);
    ghost=rect0;
    killStartime=40+rand()%(60-40);
     vDesks.resize(4);//初始化四个障碍物
    QRect rect1(120,140,80,40),rect2(400,140,80,40),rect3(140,400,40,80),rect4(430,420,40,80);//分别是左上 右上 左下 右下
     vDesks[0]=rect1;
     vDesks[1]=rect2;
     vDesks[2]=rect3;
     vDesks[3]=rect4;
    nDirection=0;//默认向下运动
    sDisplay="欢迎来到鬼房";
    tools.initempureter1(array1,ghost,ghostfloor);//初始化第一层楼的温度
    tools.initempureter2(array2,ghost,ghostfloor);//初始化第二层楼的温度
    q.resize(3);//表示选的是任务库里面的第几个
    nowassignment.resize(3);
     for(int i=0;i<3;i++)
    {
        int n=0+rand()%(5-0);
        while(number(i,q,n))
        {
            n=0+rand()%(5-0);
        }
        nowassignment[i]=assignmentstore[n];
        q[i]=n;
    }
    timer=new QTimer(this);//设定计时器
    connect(timer,SIGNAL(timeout()),this,SLOT(MainWindow_update()));
    timer->start(speed);
    timer1=new QTimer(this);
    connect(timer1,SIGNAL(timeout()),this,SLOT(MainWindow_update1()));
    timer1->start(300);
    timer2=new QTimer(this);
    connect(timer2,SIGNAL(timeout()),this,SLOT(MainWindow_update2()));
}
void MainWindow::MainWindow_update()
{
    if(nDirection==9&&numbershengmu>0)
    {
    numbershengmu--;
    tools.pausanto(adventurer,ghost,istouch);
    tools.pausanto(adventurer,ghost,havetouched);
    }
    if(iskilling==false)
    {
        timenokill++;
        if(timenokill==(killStartime)*20+1&&whichfloor==ghostfloor)
        {
            timenokill=0;
            iskilling=true;
            killOvertime=6+rand()%(15-6);
        }
        else if(timenokill==(killStartime)*20+1)
        {
            timenokill=(killStartime)*20-24;
        }
    }
    else
    {
        havekilled=true;
        timekill++;
        if(timekill==(killOvertime)*20+1)
        {
            istouch=false;
            iskilling=false;
            killStartime=20+rand()%(60-20);
            timekill=0;
            QRect rectn(ghostix,ghostiy,10,10);
            ghost=rectn;
        }
        else
        {
            if(istouch)//如果此次碰到了就让其停下
            {
                shengmu_killing=true;
                staytime++;
                if(staytime*0.05==2)
                {
                    istouch=false;
                    staytime=0;
                }
            }
            else
            {
            moveghost();
            }
        }
    }
    //加减速  加速4  减速3
    int speednow=3;
    if((nDirection==5||nDirection==6||nDirection==7||nDirection==8)&&canspeed)
    {
        havespeed=true;
        measurespeed++;
        if(measurespeed==45)
        {
            measurespeed=0;
            canspeed=false;

        }
        else
        {
            speednow=4;
        }
    }
    //为了将八个键统一起来少写代码
    if(nDirection>4)
    {
        nDirection=nDirection-4;
    }
    //加速3秒停2.25秒
    if(havespeed==true&&canspeed==false)
    {
        measurespeed++;
        if(measurespeed==61)
        {
            canspeed=true;
            measurespeed=0;
        }
    }
    //初步移动冒险家
    switch(nDirection)
    {
    case 1:
        adventurer.moveTo(adventurer.left(),adventurer.top()-speednow);
        //adventurer.setTop(adventurer.top()-speednow);
        //adventurer.setBottom(adventurer.bottom()-speednow);
        break;
    case 2:
        adventurer.moveTo(adventurer.left(),adventurer.top()+speednow);
        //adventurer.setTop(adventurer.top()+speednow);
        //adventurer.setBottom(adventurer.bottom()+speednow);
        break;
    case 3:
        adventurer.moveTo(adventurer.left()-speednow,adventurer.top());
        //adventurer.setLeft(adventurer.left()-speednow);
        //adventurer.setRight(adventurer.right()-speednow);
        break;
    case 4:
        adventurer.setLeft(adventurer.left()+speednow);
        adventurer.setRight(adventurer.right()+speednow);
        break;
    case 5:
        break;
    default:;
    }
    //冒险家四个角的坐标  adventurerSXZY是QPoint类型
    adventurerSXZY[0].setX(adventurer.left());
    adventurerSXZY[0].setY(adventurer.top());
    adventurerSXZY[1].setX(adventurer.right());
    adventurerSXZY[1].setY(adventurer.top());
    adventurerSXZY[2].setX(adventurer.left());
    adventurerSXZY[2].setY(adventurer.bottom());
    adventurerSXZY[3].setX(adventurer.right());
    adventurerSXZY[3].setY(adventurer.bottom());
    //如果在楼层一
    if(whichfloor==1)
    {
      //移动冒险家 与一系列函数结合起来
      //碰到四周墙
    if(adventurer.left()<20)
    {
        adventurer.setLeft(adventurer.left()+speednow);
        adventurer.setRight(adventurer.right()+speednow);
    }
    if(adventurer.right()>580)
    {
        adventurer.setRight(adventurer.right()-speednow);
        adventurer.setLeft(adventurer.left()-speednow);
    }
    if(adventurer.top()<20)
    {
        adventurer.setTop(adventurer.top()+speednow);
        adventurer.setBottom(adventurer.bottom()+speednow);
    }
    if(adventurer.bottom()>580)
    {
        adventurer.setBottom(adventurer.bottom()-speednow);
        adventurer.setTop(adventurer.top()-speednow);
    }
    if((adventurer.left()>140&&adventurer.right()<157)||(adventurer.left()>432&&adventurer.right()<453))
    {

    }
    else
    {
      if(isinwall(adventurer.top(),adventurer.bottom(),290,310)==1)
      {
        adventurer.setBottom(adventurer.bottom()+speednow);
        adventurer.setTop(adventurer.top()+speednow);
      }
      else if(isinwall(adventurer.top(),adventurer.bottom(),290,310)==2)
      {
        adventurer.setBottom(adventurer.bottom()-speednow);
        adventurer.setTop(adventurer.top()-speednow);
      }
    }
    if((adventurer.top()>140&&adventurer.bottom()<157)||(adventurer.top()>432&&adventurer.bottom()<453))
    {

    }
    else
    {
      if(isinwall(adventurer.left(),adventurer.right(),290,310)==1)
      {
        adventurer.setLeft(adventurer.left()+speednow);
        adventurer.setRight(adventurer.right()+speednow);
      }
      if(isinwall(adventurer.left(),adventurer.right(),290,310)==2)
      {
        adventurer.setLeft(adventurer.left()-speednow);
        adventurer.setRight(adventurer.right()-speednow);
      }
    }
    for(int i=0;i<4;i++)
    {
        int n=0;
        n=isinRect(adventurerSXZY[i],vDesks);
        if(n!=0)
        {
            move(nDirection,i,speednow);
            break;
        }
    }
    }
    //如果在楼层二就用函数
    else
    {
        floor->moveadventurer(adventurer,nDirection,speednow);
    }
    int m=0;
    QVector<QRect> ghost1;
    ghost1.resize(1);
    ghost1[0]=ghost;
    //如果在杀且碰到了人就结束
    if(iskilling)
    {
    for(int i=0;i<4;i++)
    {
        if(timekill>=6)
        {
        m=isinRect(adventurerSXZY[i],ghost1);
        if(m!=0)
        {
            blsOver=true;
            sDisplay="游戏结束";

        }
        }
    }
    }
    if(click%2==1&&adventurer.left()<=555&&adventurer.right()>=555&&adventurer.top()>=530&&adventurer.bottom()<=575&&iskilling==false&&whichfloor==1)//在那个点并且没有开启猎杀就换房间
    {
        whichfloor=1-whichfloor;
    }
    if(adventurer.left()<=40&&adventurer.right()>=40&&adventurer.top()>=0&&adventurer.bottom()<=65&&iskilling==false&&whichfloor==0)
    {
       whichfloor=1-whichfloor;
    }
    update();//paintEvent更新
}
void MainWindow:: MainWindow_update1()//开始界面持续时间
{
    times++;
    if(times==2)
    {
        sDisplay="";
    }
}
void MainWindow::keyPressEvent(QKeyEvent *event)//响应键盘
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key())
    {
    case Qt::Key_Up:nDirection=1;
        break;
    case Qt::Key_Down:nDirection=2;
        break;
    case Qt::Key_Left:nDirection=3;
        break;
    case Qt::Key_Right:nDirection=4;
        break;
    case Qt::Key_W:nDirection=5;
        break;
    case  Qt::Key_S:nDirection=6;
        break;
    case  Qt::Key_A:nDirection=7;
        break;
    case  Qt::Key_D:nDirection=8;
        break;
    case  Qt::Key_J:islight=islight+1;
        break;
    case  Qt::Key_K:nDirection=9;
        break;
    case  Qt::Key_L:
    {
        if(saltnumber<saltnumbertaken-1)
        {
        QRect x(adventurer.left(),adventurer.top(),20,20);
        salt[++saltnumber]=x;
        saltwhichfloor[saltnumber]=whichfloor;
        }
        break;
     }
    default:;
    }
}
/*void MainWindow::modifiers(QKeyEvent *key)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key())
    {
    case Qt::Key_Shift:Speedup=1;
    }
}*/
int MainWindow::isinwall(int advLT,int advRB,int wallLT,int wallRB)//是否碰到了墙
{
    if(advLT<wallRB&&advLT>wallLT)
    {
        return 1;
    }
    else if(advRB<wallRB&&advRB>wallLT)
    {
        return 2;
    }
    return 0;
}

/*int MainWindow::isinRect(int advL,int advR,int advT,int advB,int rectL,int rectR,int rectT,int rectB)
{
    if()
}*/
int MainWindow::isinRect(QPoint a,QVector<QRect> b)//是否在四个桌子里面
{
    for(int i=0;i<b.size();i++)
    {
    if((a.x()>b[i].left())&&a.x()<b[i].right()&&a.y()>b[i].top()&&a.y()<b[i].bottom())
    {
            return (i+1);
    }
    }
    return 0;
}
void MainWindow::move(int direction,int whichjiao,int speednow)//移动冒险家  撞桌子了之后
{
    if(whichjiao==0)
    {
        if(direction==3)
        {
            adventurer.setLeft(adventurer.left()+speednow);
            adventurer.setRight(adventurer.right()+speednow);
            return;
        }
        else
        {
            adventurer.setTop(adventurer.top()+speednow);
            adventurer.setBottom(adventurer.bottom()+speednow);
            return ;
        }


    }
    if(whichjiao==1)
    {
        if(direction==4)
        {
            adventurer.setLeft(adventurer.left()-speednow);
            adventurer.setRight(adventurer.right()-speednow);
            return;
        }
        else
        {
            adventurer.setTop(adventurer.top()+speednow);
            adventurer.setBottom(adventurer.bottom()+speednow);
            return ;
        }
    }
    if(whichjiao==2)
    {
        if(direction==3)
        {
            adventurer.setLeft(adventurer.left()+speednow);
            adventurer.setRight(adventurer.right()+speednow);
            return;
        }
        else
        {
            adventurer.setTop(adventurer.top()-speednow);
            adventurer.setBottom(adventurer.bottom()-speednow);
            return ;
        }
    }
    else if(whichjiao==3)
        {
            if(direction==4)
            {
                adventurer.setLeft(adventurer.left()-speednow);
                adventurer.setRight(adventurer.right()-speednow);
                return;
            }
            else
            {
                adventurer.setTop(adventurer.top()-speednow);
                adventurer.setBottom(adventurer.bottom()-speednow);
                return ;
            }
        }
}
void MainWindow::moveghost()
{
    //追踪人 不碰到墙壁  穿过墙速度减为1  在灯光下速度增为5
    if(isinsight(adventurer,ghost)&&islight%3==2)
    {
        ghostspeed=5;
    }
    else if(islight%3==0)
    {
        ghostspeed=3.5;
    }
    int q1=0,q2=0;
    if(abs(ghost.left()-adventurer.left())>abs(ghost.top()-adventurer.top()))
    {
        if(ghost.left()>adventurer.left())
        {
            QPoint t1(ghost.left()-ghostspeed,ghost.top());
            QPoint t2(ghost.left()-ghostspeed,ghost.bottom());
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.top()>adventurer.top())
                {
                    ghost.setBottom(ghost.bottom()-ghostspeed);
                    ghost.setTop(ghost.top()-ghostspeed);
                }
                else
                {
                    ghost.setBottom(ghost.bottom()+ghostspeed);
                    ghost.setTop(ghost.top()+ghostspeed);
                }
            }
            else
            {
            ghost.setLeft(ghost.left()-ghostspeed);
            ghost.setRight(ghost.right()-ghostspeed);
            }
        }
        else
        {
            QPoint t1(ghost.right()+ghostspeed,ghost.top());
            QPoint t2(ghost.right()+ghostspeed,ghost.bottom());
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.top()>adventurer.top())
                {
                    ghost.setBottom(ghost.bottom()-ghostspeed);
                    ghost.setTop(ghost.top()-ghostspeed);
                }
                else
                {
                    ghost.setBottom(ghost.bottom()+ghostspeed);
                    ghost.setTop(ghost.top()+ghostspeed);
                }
            }
            else
            {
            ghost.setLeft(ghost.left()+ghostspeed);
            ghost.setRight(ghost.right()+ghostspeed);
            }
        }
    }
    else
    {
        if(ghost.top()>adventurer.top())
        {
            QPoint t1(ghost.left(),ghost.top()-ghostspeed);
            QPoint t2(ghost.right(),ghost.top()-ghostspeed);
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.left()>adventurer.left())
                {
                    ghost.setLeft(ghost.left()-ghostspeed);
                    ghost.setRight(ghost.right()-ghostspeed);
                }
                else
                {
                    ghost.setLeft(ghost.left()+ghostspeed);
                    ghost.setRight(ghost.right()+ghostspeed);
                }
            }
            else
            {
                ghost.setTop(ghost.top()-ghostspeed);
                ghost.setBottom(ghost.bottom()-ghostspeed);
            }
        }
        else
        {
            QPoint t1(ghost.left(),ghost.bottom()+ghostspeed);
            QPoint t2(ghost.right(),ghost.bottom()+ghostspeed);
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.left()>adventurer.left())
                {
                    ghost.setLeft(ghost.left()-ghostspeed);
                    ghost.setRight(ghost.right()-ghostspeed);
                }
                else
                {
                    ghost.setLeft(ghost.left()+ghostspeed);
                    ghost.setRight(ghost.right()+ghostspeed);
                }
            }
            else
            {
               ghost.setTop(ghost.top()+ghostspeed);
              ghost.setBottom(ghost.bottom()+ghostspeed);
            }
        }
    }
    if((ghost.left()>140&&ghost.right()<157)||(ghost.left()>432&&ghost.right()<453))
    {

    }
    else
    {
        if(isinwall(ghost.top(),ghost.bottom(),290,310)==1)
        {
            ghost.setBottom(ghost.bottom()+1);
            ghost.setTop(ghost.top()+1);
        }
        else if(isinwall(ghost.top(),ghost.bottom(),290,310)==2)
        {
            ghost.setBottom(ghost.bottom()-1);
            ghost.setTop(ghost.top()-1);
        }
    }
    if((ghost.top()>140&&ghost.bottom()<157)||(ghost.top()>432&&ghost.bottom()<453))
    {

    }
    else
    {
        if(isinwall(ghost.left(),ghost.right(),290,310)==1)
        {
            ghost.setLeft(ghost.left()+1);
            ghost.setRight(ghost.right()+1);
        }
        if(isinwall(ghost.left(),ghost.right(),290,310)==2)
        {
            ghost.setLeft(ghost.left()-1);
            ghost.setRight(ghost.right()-1);
        }
    }
}
bool MainWindow::isinsight(QRect rect1,QRect rect2)//鬼是否在视野里
{
    if(rect1.left()-lightspread<=rect2.left()&&rect1.top()-lightspread<rect2.top()&&rect1.right()+lightspread>rect2.right()&&rect1.bottom()+lightspread>rect2.bottom())
    {
        return true;
    }
    return false;
}
bool MainWindow::number(int inow,QVector<int> b,int n)//判断是否产生了相同的任务
{
    for(int i=0;i<inow;i++)
    {
        if(n==b[i])
        {
            return true;
        }
    }
    return false;
}
void MainWindow::gotoassignment()//前往任务界面
{
    this->hide();
    timer->stop();
    timer1->stop();
    Assignment *assignemnt1=new Assignment();
    assignemnt1->mainwindow1=this;
    assignemnt1->show();
}

void MainWindow::traindepature()//发车
{
    if(iskilling==false||blsOver==true)
    {
    this->close();
    for(int i=0;i<3;i++)
    {
        if(ifassignmentfinished(q[i]))
            numberfinished++;
    }
    balance_account *ui_balance_account=new balance_account(money,blsOver,shengmusended,shengmustore,numberfinished,saltstore,saltnumbertaken);
    ui_balance_account->show();
    }
}
bool MainWindow::ifassignmentfinished(int n)//判断任务是否被完成
{
    switch(n)
    {
    case 0:
        if(blsOver==false&&havekilled)
            return true;
        else return false;
    case 1:
        if(havecolded)
            return true;
        else return false;
    case 2:
        if(havetouched)
            return true;
        else return false;
    case 3:
        if(shengmu_killing)//只有按了那个键并且在范围内并且鬼还在猎杀才会进去
            return true;
        else return false;
    case 4:
        if(havesalt)
            return true;
        else return false;
    default:return false;
   }
}
void MainWindow::MainWindow_update2()//在“死亡”后维持界面
{
    times1++;
    update();
}



